<?php

namespace App\Http\Model\Admin;

use Illuminate\Support\Facades\Redis;

use Illuminate\Database\Eloquent\Model;

use App\Http\Model\Admin\Record;

class ImportDsp extends Model
{
	protected $connection = 'api_mysql';
    protected $table = "collect_house_info";


    public function getDspData()
    {
    	$record = new Record();

    	$updateId = $record->getDspUpdateId();

        $list = self::where('id', '>', $updateId)->where('spread', 'like', 'ia_%')->get();
        $returnArray = [];
        // 此次请求没有获取到新数据
        if (!count($list)) {
        	return $returnArray;
        }

        foreach ($list as &$elem) {
        	$data = [];

        	$data['phone'] = $elem['username'];
        	$data['city'] = $elem['city'];
        	$data['intention'] = $elem['intention'];
        	$data['intention_price'] = $elem['intention_price'];
        	$data['house_id'] = $elem['house_id'];
        	$data['appName'] = $elem['appName'];
        	$data['create_time'] = $elem['create_time'];
        	$data['borough_name'] = $elem['borough_name'];
        	$data['borough_id'] = $elem['borough_id'];
        	$data['cityarea2_id'] = $elem['cityarea2_id'];
        	$data['cityarea2_name'] = $elem['cityarea2_name'];
        	$data['cityarea_id'] = $elem['cityarea_id'];
        	$data['cityarea_name'] = $elem['cityarea_name'];
        	$data['realname'] = $elem['realname'];
        	$data['platform_type'] = $elem['platform_type'];

        	$returnArray[] = $this->etl($data);
        }

        $newUpdateId = $list[count($list) - 1]['id'];

        $record->updateDspData($newUpdateId);

        return $returnArray;
    }

    public function etl($data)
    {
    	if (strstr( $data['intention_price'],'-') !== false) {
	    	// 清洗价格区间数据取中间数
	    	$intentionPriceList = explode('-', $data['intention_price']);

	    	$price = floor(($intentionPriceList[1] + $intentionPriceList[0]) * 0.5);

	    	$data['intention_price'] = $price;
	    }
	    else {
	    	$data['intention_price'] = 0;
	    }

    	$result = [];
    	// 做入库格式转换
    	$result['phone'] = $data['phone'];
    	$result['city'] = $data['city'];
    	$result['platform_type'] = $data['platform_type'];
    	$result['data_status'] = 2;
    	$result['source_id'] = 1;
    	$result['purpose_desc'] = $data['intention'];
    	$result['page_type'] = '二手房';
    	$result['visit_time'] = date('Y-m-d H:i:s', $data['create_time']);
    	$result['house_province'] = 0;
    	$result['house_city'] = $data['city'];
    	$result['borough_name'] = $data['borough_name'];
    	$result['borough_name_source'] = $data['borough_name'];
    	$result['borough_id'] = $data['borough_id'];
    	$result['cityarea2_id'] = $data['cityarea2_id'];
    	$result['cityarea2_name'] = $data['cityarea2_name'];
    	$result['cityarea2_name_source'] = $data['cityarea2_name'];
    	$result['cityarea_id'] = $data['cityarea_id'];
    	$result['cityarea_name'] = $data['cityarea_name'];
    	$result['cityarea_name_source'] = $data['cityarea_name'];
//    	$result['realname'] = $data['realname'];

    	return $result;
    }


}
